package com.baselibrary.libs.Utils.log;

import android.util.Log;


/**
 * Created by Administrator 于萌萌
 * 创建日期: 14:27 . 2016年06月14日
 * 描述: 方便的 log 工具类
 * <p/>
 * <p/>
 * 备注:
 */
public class LogUtils {
    //
    /** 日志输出级别NONE */
    public static final int LEVEL_NONE = 0;
    /** 日志输出级别V */
    public static final int LEVEL_VERBOSE = 1;
    /** 日志输出级别D */
    public static final int LEVEL_DEBUG = 2;
    /** 日志输出级别I */
    public static final int LEVEL_INFO = 3;
    /** 日志输出级别W */
    public static final int LEVEL_WARN = 4;
    /** 日志输出级别E */
    public static final int LEVEL_ERROR = 5;

    public static final String SEPARATOR = ",";

    /** 日志输出时的TAG */
    private static String mTag = "yumengmeng";
    /** 是否允许输出log */
  // private static int mDebuggable = BuildConfig.debug_level;

            //正式
  // private static int mDebuggable = LogUtils.LEVEL_NONE;
            //debug
  private static int mDebuggable = LogUtils.LEVEL_INFO;
  //private static int mDebuggable = LogUtils.LEVEL_NONE;
   //  private static int mDebuggable = LogUtils.LEVEL_INFO;

    /** 用于记时的变量 */
    private static long mTimestamp = 0;
    /** 写文件的锁对象 */
    private static final Object mLogLock = new Object();

    /** 以级别为 d 的形式输出LOG */
    public static void v(String msg) {
            if (mDebuggable >= LEVEL_VERBOSE) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
                Log.v(mTag, getLogInfo(stackTraceElement,msg));
        }

    }

    /** 以级别为 d 的形式输出LOG */
    public static void d(String msg) {
            if (mDebuggable >= LEVEL_DEBUG) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
                Log.d(mTag, getLogInfo(stackTraceElement,msg));
            }
    }

    /** 以级别为 i 的形式输出LOG */
    public static void i(String msg) {
            if (mDebuggable >= LEVEL_INFO) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
                Log.i(mTag, getLogInfo(stackTraceElement,msg));
        }
    }
//    /** 以级别为 i 的形式输出LOG 增强版 带隔断的*/
//    public static void i(String msg, String s1, String s11, String s2, String s22 , String s3 , String s33) {
//            if (mDebuggable >= LEVEL_INFO) {
//                Log.i(mTag, msg + "\n" + s1 + "\t" + s11 + "\n" + s2 + "\t" + s22 + "\n" + s3 + "\t" + s33 + "\n");
//        }
//    }

    /** 以级别为 w 的形式输出LOG */
    public static void w(String msg) {
            if (mDebuggable >= LEVEL_WARN) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
                Log.w(mTag, getLogInfo(stackTraceElement,msg));
        }
    }

    /** 以级别为 w 的形式输出Throwable */
    public static void w(Throwable tr) {
            if (mDebuggable >= LEVEL_WARN) {
                Log.w(mTag, "", tr);
        }
    }

    /** 以级别为 w 的形式输出LOG信息和Throwable */
    public static void w(String msg, Throwable tr) {
        if (mDebuggable >= LEVEL_WARN && null != msg) {
            Log.w(mTag, msg, tr);
        }
    }

    /** 以级别为 e 的形式输出LOG */
    public static void e(String msg) {
            if (mDebuggable >= LEVEL_ERROR) {
                StackTraceElement stackTraceElement = Thread.currentThread().getStackTrace()[3];
                Log.e(mTag, getLogInfo(stackTraceElement,msg));
        }
    }

    /** 以级别为 e 的形式输出Throwable */
    public static void e(Throwable tr) {
            if (mDebuggable >= LEVEL_ERROR) {
                Log.e(mTag, "", tr);
        }
    }

    /** 以级别为 e 的形式输出LOG信息和Throwable */
    public static void e(String msg, Throwable tr) {
            if (mDebuggable >= LEVEL_ERROR && null != msg) {
                Log.e(mTag, msg, tr);
        }
    }

    /**
     * 分段打印出较长log文本
     * @param log        原log文本
     * @param showCount  规定每段显示的长度（最好不要超过eclipse限制长度）
     */
    public static void showLogCompletion(String log,int showCount){
        if (mDebuggable >= LEVEL_ERROR) {
            if (log.length() > showCount) {
                String show = log.substring(0, showCount);
//          System.out.println(show);
                Log.i(mTag, show + "");
                if ((log.length() - showCount) > showCount) {//剩下的文本还是大于规定长度
                    String partLog = log.substring(showCount, log.length());
                    showLogCompletion(partLog, showCount);
                } else {
                    String surplusLog = log.substring(showCount, log.length());
//              System.out.println(surplusLog);
                    Log.i(mTag, surplusLog + "");
                }

            } else {
//          System.out.println(log);
                Log.i(mTag, log + "");
            }
        }
    }

    /**
     * 获取默认的TAG名称.
     * 比如在MainActivity.java中调用了日志输出.
     * 则TAG为MainActivity
     */
    public static String getDefaultTag(StackTraceElement stackTraceElement) {
        String fileName = stackTraceElement.getFileName();
        String stringArray[] = fileName.split("\\.");
        String tag = stringArray[0];
        return tag;
    }

    /**
     * 输出日志所包含的信息
     */
    public static String getLogInfo(StackTraceElement stackTraceElement,String msg) {
        StringBuilder logInfoStringBuilder = new StringBuilder();
        // 获取线程名
        String threadName = Thread.currentThread().getName();
        // 获取线程ID
        long threadID = Thread.currentThread().getId();
        // 获取文件名.即xxx.java
        String fileName = stackTraceElement.getFileName();
        // 获取类名.即包名+类名
        String className = stackTraceElement.getClassName();
        // 获取方法名称
        String methodName = stackTraceElement.getMethodName();
        // 获取生日输出行数
        int lineNumber = stackTraceElement.getLineNumber();

        logInfoStringBuilder.append("====================开始====================== "+"\n");
        logInfoStringBuilder.append("threadID 线程id =" + threadID).append(SEPARATOR+"\n");
        logInfoStringBuilder.append("threadName 线程名称 =" + threadName).append(SEPARATOR+"\n");
        logInfoStringBuilder.append("fileName 文件名 =" + fileName).append(SEPARATOR+"\n");
        logInfoStringBuilder.append("className 类文件路径 =" + className).append(SEPARATOR+"\n");
        logInfoStringBuilder.append("methodName 方法名 =" + methodName).append(SEPARATOR+"\n");
        logInfoStringBuilder.append("lineNumber 打印行号 =" + lineNumber+"\n");
        logInfoStringBuilder.append("\n内容是 "+"\n\n\n"+msg+"\n\n\n");
        logInfoStringBuilder.append("====================结束====================== "+"\n");
        return logInfoStringBuilder.toString();
    }
}
